<script>
	import homeServices from './service/homeService.js'
	export default {
		onLaunch: function() {
			// #ifdef APP-PLUS
			//获取当前版本号
			plus.runtime.getProperty(plus.runtime.appid, (info) => {
				this.version = info.version;
				console.log(this.version); 
				this.goUpdate()
			})
			
			//消息推送
			// plus.push.addEventListener('receive', function(message) {
			// 	// plus.nativeUI.toast('push receive');
			// 	const Audio = uni.createInnerAudioContext();
			// 	Audio.autoplay = true;
			// 	Audio.src = '/static/iconShow/music.mp3'; //音频地址  
			// 	Audio.play(); //执行播放  
			// 	Audio.onError((res) => {
			// 		console.log(res.errMsg);
			// 		console.log(res.errCode);
			// 	});
			// 	Audio.onPause(function() {
			// 		console.log('end');
			// 		Audio.destroy();
			// 	});
			// });
			
			// #endif


		},
		onShow: function() {

		},
		onHide: function() {

		},
		methods: {
			//升级
			async goUpdate() {
				// console.log(this.version); 
				let params = {
					oldVersion: this.version,
					type: 1101
				}
				let res = await homeServices.getVersion(params)
				if (res.status == 200) {
					console.log(res);
					//整包更新
					if (res.data.status === 1) {
						uni.showModal({
							title: "更新提示",
							content: res.data.versionInfo,
							showCancel: false,
							success: (conf) => {
								if (conf.confirm) {
									// plus.runtime.openURL(res.data.url);
									uni.showToast({
										icon: "none",
										mask: true,
										title: '有新的版本发布，程序已启动自动更新',
										duration: 5000,
									});
									var downloadApkUrl = res.data.downloadUrl;
									var dtask = plus.downloader.createDownload(downloadApkUrl, {},
										function(d, status) {
											// 下载完成  
											if (status == 200) {
												plus.runtime.install(plus.io.convertLocalFileSystemURL(
													d.filename), {}, {}, function(error) {
													uni.showToast({
														title: '安装失败',
														duration: 1500
													});
												})
											} else {
												uni.showToast({
													title: '更新失败',
													duration: 1500
												});
											}
										});
									try {
										dtask.start(); // 开启下载的任务
										var prg = 0;
										var showLoading = plus.nativeUI.showWaiting("正在下载"); //创建一个showWaiting对象 
										dtask.addEventListener('statechanged', function(task, status) {
											// console.log('自动更新测试1：', task)
											// console.log('自动更新测试2：', status)
											// 给下载任务设置一个监听 并根据状态  做操作
											switch (task.state) {
												case 1:
													showLoading.setTitle("正在下载");
													break;
												case 2:
													showLoading.setTitle("已连接到服务器");
													break;
												case 3:
													prg = parseInt((parseFloat(task.downloadedSize) / parseFloat(task.totalSize)) *100);
													showLoading.setTitle("  正在下载" + prg + "%  ");
													break;
												case 4:
													plus.nativeUI.closeWaiting();
													//下载完成
													break;
											}
										});
									} catch (err) {
										console.log('错误信息', err)
										plus.nativeUI.closeWaiting();
									}


									// dtask.start();
								}
							}
						})
					}
					//热更新
					if (res.data.status === 2) {
						uni.showModal({
							title: "更新提示",
							content: res.data.versionInfo,
							showCancel: false,
							success: (conf) => {
								if (conf.confirm) {
									uni.showToast({
										icon: "none",
										mask: true,
										title: '有新的版本发布，程序已启动自动更新',
										duration: 5000,
									});
									var downloadApkUrl = res.data.downloadUrl;
									var dtask = plus.downloader.createDownload(downloadApkUrl, {},
										function(d, status) {
											// 下载完成  
											if (status == 200) {
												plus.runtime.install(d.filename, {
													force: true
												}, function() {
													uni.showToast({
														title: '升级完成',
														duration: 1000
													})

													setTimeout(() => {
														plus.runtime.restart();
													}, 1500)
												}, function(e) {
													uni.showToast({
														title: '升级失败',
														duration: 1000
													})
													console.error('install fail...');
												});

											} else {
												uni.showToast({
													title: '更新失败',
													duration: 1500
												});
											}
										});
									dtask.start();

								}
							}
						})
					}




				}

			},
		}
	}
</script>

<style lang="scss">
	page {
		background: transparent;
	}

	@import "uview-ui/index.scss";
</style>
